package com.action.gulimall.cart.controller;

import com.action.common.utils.R;
import com.action.gulimall.cart.service.CartService;
import com.action.gulimall.cart.vo.CartItemVo;
import com.action.gulimall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author sl shilun217@qq.com
 * @Deacription 购物车相关
 * @date 2021/06/29/14:04
 **/
@Controller
public class CartController {

    @Autowired
    CartService cartService;
    @GetMapping("/checkedCartItems")
    @ResponseBody
    public R getCheckedCartItems() throws Exception {
        List<CartItemVo> cartItemVoList = cartService.getUserCartItems();
        return R.ok().setData(cartItemVoList);
    }

    //添加购物项到购物车
    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num,
                              RedirectAttributes attributes) throws ExecutionException, InterruptedException {
        attributes.addAttribute("skuId",skuId);
        cartService.addCartItem(skuId,num);
        return "redirect:http://cart.gulimall.com/addSuccessPage.html";
    }

    //跳转到添加成功页面，防止重复提交
    @GetMapping("/addSuccessPage.html")
    public String addSuccessPage(@RequestParam("skuId") Long skuId,
                                 Model model){
        //重定向到成功页面。再次查询购物车数据即可
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }

    //查询购物车列表
    @GetMapping("/cart.html")
    public String cartList(Model model){
        CartVo cartVo =  cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    //购物项状态是否选中
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                                      @RequestParam("checked") Integer checked){
        cartService.changeCartItemCheck(skuId,checked);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    //改变购物项数量
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num){
        cartService.changeCartItemCount(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    //删除购物项
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteCartItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    //清空购物车

    //查看购物车数量
    @ResponseBody
    @GetMapping("/cartNum")
    public R cartNum(){
        Long cartNum = cartService.cartNum();
        return R.ok().setData(cartNum);
    }

}
